<template>
	<view>
		<view class="input_pwd">
			<view class="tit">
				{{$t("user.update-old-pwd")}}:
			</view>
			<view class="">
				<u-input v-model="value1" :type="type" :border="border" :password-icon="passwordIcon" :placeholder="$t('user.update.content')"/>
			</view>
		</view>

		<view class="input_pwd"  >
			<view class="tit">
				{{$t("user.update-new-pwd")}}:
			</view>
			<view class="">
				<u-input v-model="value2" :type="type" :border="border" :password-icon="passwordIcon" :placeholder="$t('user.update.content')" />
			</view>
		</view>
		
		<view class="input_pwd" >
			<view class="tit">
				{{$t("user.update-new-re-pwd")}}:
			</view>
			<view class="">
				<u-input v-model="value3" :type="type" :border="border" :password-icon="passwordIcon" :placeholder="$t('user.update.content')"/>
			</view>
		</view>
		
		<view class="btn">
			<u-button type="primary" size="medium" @click="submit">{{$t("public.confirm")}}</u-button>
		</view>
		
	<!-- 	<view class="btn" v-if="typePwd == 2">
			<u-button type="primary" size="medium"  @click="submitEdit">确定</u-button>
		</view> -->
		
	</view>
</template>
<script>
	import {  rulePwd,editPassword } from '@/api/new.js';
	import {
		userEdit,
		getLogout
	} from '@/api/user.js';
	export default {

		data() {
			return {
				typePwd:1,
				value1: '',
				value2: '',
				value3: '',
				type: 'password',
				passwordIcon: true,
				border: true,
			}
		},
		methods:{
			onLoad() {
				uni.setNavigationBarTitle({
					title: this.$t("user.update-pwd")
				})
			},
			submitEdit(){
				let that = this
				let data = {
					oldPwd:that.value1,
					newPwd:this.value3
				}
				if(that.value2 == that.value3){
					editPassword(data).then(res=>{
						console.log(res,'res');
						// that.typePwd = 2
						that.$util.Tips({
							title: res.message=="旧密码错误"?this.$t('user.update.old-pwd-fail'):this.$t('auctionTips.2009')
						})
						setTimeout(()=>{
							getLogout()
								.then(res => {
									that.$store.commit("LOGOUT");
									uni.reLaunch({
										url: '/pages/users/login/index'
									});
								})
								.catch(err => {
									console.log(err);
								});
						},1500)
					}).catch(res => {
						that.value1 = ''
						that.$util.Tips({
							title: res=="旧密码错误"?this.$t('user.update.old-pwd-fail'):this.$t('auctionTips.2009')
						})
					});
				}else{
					that.$util.Tips({
						title: this.$t("user.update-pwd-check")
					})
				}
				
				
			},
			submit(){
				let that = this
				let data = {
					oldPwd:this.value1
				}
				rulePwd(data).then(res=>{
					console.log(res,'res');
					// this.typePwd = 2
					let data1 = {
						oldPwd:that.value1,
						newPwd:this.value3
					}
					if(that.value2 == that.value3){
						editPassword(data1).then(res=>{
							console.log(res,'res');
							// that.typePwd = 2
							that.$util.Tips({
								title: that.$t('user.update.old-pwd-success')
							})
							setTimeout(()=>{
								getLogout()
									.then(res => {
										that.$store.commit("LOGOUT");
										uni.reLaunch({
											url: '/pages/users/login/index'
										});
									})
									.catch(err => {
										console.log(err);
									});
							},1500)
						}).catch(res => {
							that.value1 = ''
							that.$util.Tips({
								title: res
							})
						});
					}else{
						that.$util.Tips({
							title: this.$t("user.update-pwd-check")
						})
					}
					
					
					
				}).catch(res => {
					this.value1 = ''
					that.$util.Tips({
						title: res=="旧密码错误"?this.$t('user.update.old-pwd-fail'):this.$t('auctionTips.2009')
					})
				});
				// this.typePwd = 2
			
			
			}
		}
	}
</script>
<style lang="scss">
	.input_pwd {
		display: flex;
		margin: 45px 10px 20px;
		align-items: center;

		.tit {
			font-weight: bold;
			font-size: 16px;
			margin-right: 10px;
			flex:1
		}
	}

	.placeholder {
		// color: #b9b9bc;
		border: 1px solid #ccc;
		padding: 20px;
	}

	.btn {
		display: flex;
		justify-content: center;
		margin-top: 50px;
	}
</style>